package com.demoo.zjsu.service.smartwebsite;

import com.demoo.zjsu.dao.smartwebsite.SmartWebDAO;
import com.demoo.zjsu.model.smartwebsite.PageTwo;
import com.demoo.zjsu.model.smartwebsite.PageTwoContent;
import com.demoo.zjsu.util.smartwebsite.WebsiteUtil;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jshjsh on 2016/8/17.
 */
@Service
public abstract class PageFakeListFactoryExtend extends PageFactoryExtend{

    @Autowired
    SmartWebDAO dao;

    @Override
    public void resolver() {
        Page page = getPage();
        String[] categories = page.getCategories();
        String url = page.getUrl();

        Document document = null;
        try {
            document = WebsiteUtil.getDocByURL(url);
            if (document == null) {
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Elements> elementsList = getMainList(document);
        int len = categories.length;
        List<PageTwo> pageTwos = new ArrayList<PageTwo>();
        List<PageTwoContent> pageTwoContents = new ArrayList<PageTwoContent>();
        for (int i = 0; i < len; i++) {
            Elements elements = elementsList.get(i);
            String category = categories[i];
            filterElse(elements);
            packageContent(elements, pageTwos, pageTwoContents, category);
        }
        dao.insertListPageTwo(pageTwos);
        dao.insertListPageTwoContent(pageTwoContents);
    }




    /**
     * 这个类的继承类里不需要完成这个方法，改写成另一个抽象方法
     * @param document
     * @return
     */
    @Override
    public Elements getMain(Document document) {
        return null;
    }


    public abstract List<Elements> getMainList(Document document);
}
